package com.itcv;


import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;

import static org.objectweb.asm.Opcodes.ASM6;

public class TestTransformer implements ClassFileTransformer {
    @Override
    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
        System.out.println(className);
        String currentClassName = className.replace("/",".");
        System.out.println(currentClassName);
        if(currentClassName.equals("com.itcv.App")){
            try {
                ClassReader clr = new ClassReader(className);
                ClassWriter clw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
                ClassVisitor clv = new SkyWorthClassVisitor(clw);
                clr.accept(clv,ClassReader.EXPAND_FRAMES);
                byte [] bytes = clw.toByteArray();
                File file = new File("D:\\work\\idea\\study\\agentDemo\\app\\src\\main\\java\\com\\itcv\\Test.class");

                FileOutputStream fos = new FileOutputStream("D:\\work\\idea\\study\\agentDemo\\app\\src\\main\\java\\com\\itcv\\Test.class");
                fos.write(bytes);
                fos.close();
                return bytes;

               // return classfileBuffer;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }else {
            return classfileBuffer;
        }


    }
}


